package com.sky.controller.admin;

import com.sky.dto.AdminLoginDTO;
import com.sky.entity.Admin;
import com.sky.result.Result; // 使用sky-common模块的Result类，成功状态码为1
import com.sky.service.AdminService;
import com.sky.vo.AdminLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/admin")
@Api(tags = "管理员相关接口")
public class AdminController {

    private static final Logger log = LoggerFactory.getLogger(AdminController.class);

    @Autowired
    private AdminService adminService;

    @ApiOperation("管理员登录")
    @PostMapping("/login")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO) {
        AdminLoginVO adminLoginVO = adminService.login(adminLoginDTO);
        log.info("管理员登录成功，用户名：{}", adminLoginDTO.getUsername());
        return Result.success(adminLoginVO);
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public Result<String> logout() {
        adminService.logout();
        log.info("管理员退出登录成功");
        return Result.success("退出成功");
    }

    @ApiOperation("管理员分页查询")
    @GetMapping("/page")
    public Result<Map<String, Object>> selectByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String username,
            String role,
            String status) {

        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("pageSize", pageSize);
        map.put("username", username);
        map.put("role", role);
        map.put("status", status);

        Map<String, Object> result = adminService.selectByPage(map);
        log.info("管理员分页查询成功，参数：page={}, pageSize={}, username={}, role={}, status={}",
                page, pageSize, username, role, status);
        return Result.success(result);
    }

    @ApiOperation("新增管理员")
    @PostMapping
    public Result<String> insert(@RequestBody Admin admin) {
        adminService.insert(admin);
        log.info("新增管理员成功，用户名：{}", admin.getUsername());
        return Result.success("新增成功");
    }

    @ApiOperation("根据id查询管理员")
    @GetMapping("/{id}")
    public Result<Admin> selectById(@PathVariable Long id) {
        Admin admin = adminService.selectById(id);
        log.info("根据id查询管理员成功，id：{}", id);
        return Result.success(admin);
    }

    @ApiOperation("编辑管理员信息")
    @PutMapping
    public Result<String> update(@RequestBody Admin admin) {
        adminService.update(admin);
        log.info("编辑管理员信息成功，id：{}", admin.getId());
        return Result.success("更新成功");
    }

    @ApiOperation("修改管理员密码")
    @PutMapping("/password")
    public Result<String> updatePassword(
            @RequestParam Long adminId,
            @RequestParam String oldPassword,
            @RequestParam String newPassword) {

        adminService.updatePassword(adminId, oldPassword, newPassword);
        log.info("修改管理员密码成功，id：{}", adminId);
        return Result.success("密码修改成功");
    }

    @ApiOperation("启用禁用管理员")
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(
            @PathVariable String status,
            @RequestParam Long id) {

        adminService.updateStatus(id, status);
        log.info("更新管理员状态成功，id：{}, 状态：{}", id, status);
        return Result.success("状态更新成功");
    }

    @ApiOperation("根据id删除管理员")
    @DeleteMapping
    public Result<String> deleteById(@RequestParam Long id) {
        adminService.deleteById(id);
        log.info("删除管理员成功，id：{}", id);
        return Result.success("删除成功");
    }
}
